﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.DALFactory;

namespace KPIS.GERP.GHRMS.BLL
{
    public sealed class CommandDetailBLL : ICommandDetailBLL
    {
        private static readonly ICommandDetailDAL DAL = DataAccess.CreateCommandDetails();

        private CommandDetailInfo dtlsInfo = new CommandDetailInfo();
        private CommandInfo cmdInfo = new CommandInfo();
        private EmployeeInfo empInfo = new EmployeeInfo();
        private JobSiteInfo jsInfo = new JobSiteInfo();
        private PositionInfo posInfo = new PositionInfo();
        private ClassInfo classInfo = new ClassInfo();

        private JobSiteInfo jsInfo2 = new JobSiteInfo();
        private PositionInfo posInfo2 = new PositionInfo();
        private ClassInfo classInfo2 = new ClassInfo();
        private ClassInfo classInfo3 = new ClassInfo();
        private PositionTypeInfo ptInfo = new PositionTypeInfo();

        private HisEducationInfo eduInfo = new HisEducationInfo();
        private MovmentInfo movInfo = new MovmentInfo();

        public IList<CommandDetailInfo> ListCommandDetail()
        {
            return DAL.ListCommandDetail(new CommandDetailInfo(0));
        }
        public IList<CommandDetailInfo> ListCommandDetail(int Commands)
        {
            if (Commands > 0)
                cmdInfo.Sequence = Commands;
            
            dtlsInfo.Command = cmdInfo;
            return DAL.ListCommandDetail(dtlsInfo);
        }
        public IList<CommandDetailInfo> ListCommandDetailBySequence(int Sequence)
        {
            return DAL.ListCommandDetail(new CommandDetailInfo(Sequence));
        }
        public IList<CommandDetailInfo> ListCommandDetailByEmployee(int Employee)
        {
            if (Employee > 0)
                empInfo.Sequence = Employee;

            dtlsInfo.Employee = empInfo;
            return DAL.ListCommandDetail(dtlsInfo);
        }
        public IList<CommandDetailInfo> ListCommandPosition(int Employee)
        {
            if (Employee > 0)
                empInfo.Sequence = Employee;

            dtlsInfo.Employee = empInfo;
            return DAL.ListCommandPosition(dtlsInfo);
        }
        public IList<CommandDetailInfo> ListCommandSalary(int Employee)
        {
            if (Employee > 0)
                empInfo.Sequence = Employee;

            dtlsInfo.Employee = empInfo;
            return DAL.ListCommandSalary(dtlsInfo);
        }
        public DataSet LoadCommandDetails(Nullable<int> Sequence, Nullable<int> CommandSeq, Nullable<int> EmployeeSeq)
        {
            if (Sequence.HasValue)
                dtlsInfo.Sequence = (int)Sequence;

            if (CommandSeq.HasValue)
            {
                cmdInfo.Sequence = (int)CommandSeq;
                dtlsInfo.Command = cmdInfo;
            }

            if (EmployeeSeq.HasValue)
            {
                empInfo.Sequence = (int)EmployeeSeq;
                dtlsInfo.Employee = empInfo;
            }

            return DAL.LoadCommandDetails(dtlsInfo);
        }
        public DataSet LoadCommandDetails(Nullable<int> EmployeeSeq, string CommandGroup)
        {
            if (EmployeeSeq.HasValue)
            {
                empInfo.Sequence = (int)EmployeeSeq;
                dtlsInfo.Employee = empInfo;
            }
            dtlsInfo.Group = CommandGroup;

            return DAL.LoadCommandDetails(dtlsInfo);
        }
        public DataSet LoadCommandDetailReports(Nullable<int> CommandSeq)
        {
            if (CommandSeq.HasValue)
            {
                cmdInfo.Sequence = (int)CommandSeq;
                dtlsInfo.Command = cmdInfo;
            }
            return DAL.LoadCommandDetailReports(dtlsInfo);
        }
        public void Insert(int cmd_seq, int emp_seq, Nullable<DateTime> start_date, Nullable<int> new_pos_seq
            , string new_pos_ref, string new_pos_name, Nullable<int> new_class_seq, Nullable<int> new_job_site_seq
            , string new_job_site_name, Nullable<Double> new_salary, Nullable<Double> old_salary
            , Nullable<Double> prm_base, Nullable<Double> prm_percent, Nullable<Double> prm_amount
            , string Remarks, int CreatedBy)
        {
            try
            {
                cmdInfo.Sequence = cmd_seq;
                empInfo.Sequence = emp_seq;
                posInfo.Sequence = new_pos_seq;
                jsInfo.Sequence = new_job_site_seq;
                classInfo.Sequence = (int)new_class_seq;

                dtlsInfo.Command = cmdInfo;
                dtlsInfo.Employee = empInfo;
                dtlsInfo.NewPosition = posInfo;
                dtlsInfo.NewJobSite = jsInfo;
                dtlsInfo.NewClass = classInfo;
                dtlsInfo.StartDate = start_date;
                dtlsInfo.NewPositionNo = new_pos_ref;
                dtlsInfo.NewPositionName = new_pos_name;
                dtlsInfo.NewJobSiteName = new_job_site_name;
                dtlsInfo.NewSalary = (double)new_salary;
                dtlsInfo.OldSalary = (double)old_salary;

                dtlsInfo.PromotionBase = (double)prm_base;
                dtlsInfo.PromotionPercent = (double)prm_percent;
                dtlsInfo.PromotionAmount = (double)prm_amount;

                dtlsInfo.Remarks = Remarks;
                dtlsInfo.CreatedBy = CreatedBy;

                DAL.Insert(dtlsInfo);

            }
            catch
            {
                throw new Exception("");
            }
        }
        public void Insert(int cmd_seq, Nullable<int> emp_seq, Nullable<DateTime> start_date, Nullable<int> new_pos_seq
            , string new_pos_ref, string new_pos_name, Nullable<int> new_class_seq, Nullable<int> new_job_site_seq
            , string new_job_site_name, Nullable<Double> new_salary, Nullable<Double> old_salary
            , Nullable<int> old_pos_seq, string old_pos_ref, string old_pos_name
            , Nullable<int> old_class_seq, Nullable<int> old_job_site_seq, string old_job_site_name
            , Nullable<int> ent_no, string ent_pos_name, Nullable<int> ent_pos_type_seq, Nullable<int> ent_class_seq
            , string ent_job_site_name, Nullable<DateTime> ent_date, Nullable<int> tmp_emp_seq, Nullable<int> tmp_edu_seq
            , Nullable<int> edu_his_seq, Nullable<int> movment_seq, Nullable<DateTime> curr_pos_class_date, string Remarks, int CreatedBy)
        {
            try
            {                
                if (new_pos_seq.HasValue)
                {
                    posInfo.Sequence = new_pos_seq;
                    dtlsInfo.NewPosition = posInfo;
                }
                if (new_job_site_seq.HasValue)
                {
                    jsInfo.Sequence = new_job_site_seq;
                    dtlsInfo.NewJobSite = jsInfo;
                }
                if (new_class_seq.HasValue)
                {
                    classInfo.Sequence = (int)new_class_seq;
                    dtlsInfo.NewClass = classInfo;
                }

                if (old_pos_seq.HasValue)
                {
                    posInfo2.Sequence = old_pos_seq;
                    dtlsInfo.OldPosition = posInfo2;
                }
                if (old_job_site_seq.HasValue)
                {
                    jsInfo2.Sequence = old_job_site_seq;
                    dtlsInfo.OldJobSite = jsInfo2;
                }
                if (old_class_seq.HasValue)
                {
                    classInfo2.Sequence = (int)old_class_seq;
                    dtlsInfo.OldClass = classInfo2;
                }

                if (edu_his_seq.HasValue)
                {
                    eduInfo.Sequence = (int)edu_his_seq;
                    dtlsInfo.HisEducations = eduInfo;
                }
                if (movment_seq.HasValue)
                {
                    movInfo.Sequence = (int)movment_seq;
                    dtlsInfo.Movment = movInfo;
                }
                if (ent_pos_type_seq.HasValue)
                {
                    ptInfo.Sequence = (int)ent_pos_type_seq;
                    dtlsInfo.EntPositionType = ptInfo;
                }
                if (ent_class_seq.HasValue)
                {
                    classInfo3.Sequence = (int)ent_class_seq;
                    dtlsInfo.EntClass = classInfo3;
                }
                if (emp_seq.HasValue)
                {
                    empInfo.Sequence = (int)emp_seq;
                    dtlsInfo.Employee = empInfo;
                }
                cmdInfo.Sequence = cmd_seq;                
                dtlsInfo.Command = cmdInfo;                              

                dtlsInfo.StartDate = start_date;
                dtlsInfo.NewPositionNo = new_pos_ref;
                dtlsInfo.NewPositionName = new_pos_name;
                dtlsInfo.NewJobSiteName = new_job_site_name;
                dtlsInfo.NewSalary = new_salary.HasValue ? (double)new_salary : 0;
                dtlsInfo.OldSalary = old_salary.HasValue ? (double)old_salary : 0;
                dtlsInfo.OldPositionNo = old_pos_ref;
                dtlsInfo.OldPositionName = old_pos_name;
                dtlsInfo.OldJobSiteName = old_job_site_name;
                dtlsInfo.EntNo = ent_no;
                dtlsInfo.EntPositionName = ent_pos_name;
                dtlsInfo.EntJobSiteName = ent_job_site_name;
                dtlsInfo.EntDate = ent_date;
                dtlsInfo.CurrentPosClassDate = curr_pos_class_date;

                dtlsInfo.TmpEmployee = tmp_emp_seq;
                dtlsInfo.TmpEducate = tmp_edu_seq;

                dtlsInfo.Remarks = Remarks;
                dtlsInfo.CreatedBy = CreatedBy;

                DAL.Insert(dtlsInfo);

            }
            catch
            {
                throw new Exception("");
            }
        }
        public void Update(int cmd_dtls_seq, int emp_seq, Nullable<DateTime> start_date, Nullable<int> new_pos_seq
            , string new_pos_ref, string new_pos_name, Nullable<int> new_class_seq, Nullable<int> new_job_site_seq
            , string new_job_site_name, Nullable<Double> new_salary, Nullable<Double> old_salary
            , Nullable<Double> prm_base, Nullable<Double> prm_percent, Nullable<Double> prm_amount
            , string Remarks, int UpdatedBy)
        {
            try
            {
                empInfo.Sequence = emp_seq;
                posInfo.Sequence = new_pos_seq;
                jsInfo.Sequence = new_job_site_seq;
                classInfo.Sequence = (int)new_class_seq;

                dtlsInfo.Sequence = cmd_dtls_seq;
                dtlsInfo.Employee = empInfo;
                dtlsInfo.NewPosition = posInfo;
                dtlsInfo.NewJobSite = jsInfo;
                dtlsInfo.NewClass = classInfo;
                dtlsInfo.StartDate = start_date;
                dtlsInfo.NewPositionNo = new_pos_ref;
                dtlsInfo.NewPositionName = new_pos_name;
                dtlsInfo.NewJobSiteName = new_job_site_name;
                dtlsInfo.NewSalary = (double)new_salary;
                dtlsInfo.OldSalary = (double)old_salary;

                dtlsInfo.PromotionBase = (double)prm_base;
                dtlsInfo.PromotionPercent = (double)prm_percent;
                dtlsInfo.PromotionAmount = (double)prm_amount;
                dtlsInfo.Remarks = Remarks;
                dtlsInfo.UpdatedBy = UpdatedBy;

                DAL.Update(dtlsInfo);
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void Update(int cmd_dtls_seq, Nullable<int> emp_seq, Nullable<DateTime> start_date, Nullable<int> new_pos_seq
            , string new_pos_ref, string new_pos_name, Nullable<int> new_class_seq, Nullable<int> new_job_site_seq
            , string new_job_site_name, Nullable<Double> new_salary, Nullable<Double> old_salary
            , Nullable<int> old_pos_seq, string old_pos_ref, string old_pos_name
            , Nullable<int> old_class_seq, Nullable<int> old_job_site_seq, string old_job_site_name
            , Nullable<int> ent_no, string ent_pos_name, Nullable<int> ent_pos_type_seq, Nullable<int> ent_class_seq
            , string ent_job_site_name, Nullable<DateTime> ent_date, Nullable<int> tmp_emp_seq, Nullable<int> tmp_edu_seq
            , Nullable<int> edu_his_seq, Nullable<int> movment_seq, Nullable<DateTime> curr_pos_class_date, string Remarks, int UpdatedBy)
        {
            try
            {                
                if (new_pos_seq.HasValue)
                {
                    posInfo.Sequence = new_pos_seq;
                    dtlsInfo.NewPosition = posInfo;
                }
                if (new_job_site_seq.HasValue)
                {
                    jsInfo.Sequence = new_job_site_seq;
                    dtlsInfo.NewJobSite = jsInfo;
                }
                if (new_class_seq.HasValue)
                {
                    classInfo.Sequence = (int)new_class_seq;
                    dtlsInfo.NewClass = classInfo;
                }

                if (old_pos_seq.HasValue)
                {
                    posInfo2.Sequence = old_pos_seq;
                    dtlsInfo.OldPosition = posInfo2;
                }
                if (old_job_site_seq.HasValue)
                {
                    jsInfo2.Sequence = old_job_site_seq;
                    dtlsInfo.OldJobSite = jsInfo2;
                }
                if (old_class_seq.HasValue)
                {
                    classInfo2.Sequence = (int)old_class_seq;
                    dtlsInfo.OldClass = classInfo2;
                }

                if (edu_his_seq.HasValue)
                {
                    eduInfo.Sequence = (int)edu_his_seq;
                    dtlsInfo.HisEducations = eduInfo;
                }
                if (movment_seq.HasValue)
                {
                    movInfo.Sequence = (int)movment_seq;
                    dtlsInfo.Movment = movInfo;
                }
                if (ent_pos_type_seq.HasValue)
                {
                    ptInfo.Sequence = (int)ent_pos_type_seq;
                    dtlsInfo.EntPositionType = ptInfo;
                }
                if (ent_class_seq.HasValue)
                {
                    classInfo3.Sequence = (int)ent_class_seq;
                    dtlsInfo.EntClass = classInfo3;
                }
                if (emp_seq.HasValue)
                {
                    empInfo.Sequence = (int)emp_seq;
                    dtlsInfo.Employee = empInfo;
                }

                dtlsInfo.Sequence = cmd_dtls_seq;
                dtlsInfo.StartDate = start_date;
                dtlsInfo.NewPositionNo = new_pos_ref;
                dtlsInfo.NewPositionName = new_pos_name;
                dtlsInfo.NewJobSiteName = new_job_site_name;
                dtlsInfo.NewSalary = new_salary.HasValue ? (double)new_salary : 0;
                dtlsInfo.OldSalary = old_salary.HasValue ? (double)old_salary : 0;
                dtlsInfo.OldPositionNo = old_pos_ref;
                dtlsInfo.OldPositionName = old_pos_name;
                dtlsInfo.OldJobSiteName = old_job_site_name;
                dtlsInfo.EntNo = ent_no;
                dtlsInfo.EntPositionName = ent_pos_name;
                dtlsInfo.EntJobSiteName = ent_job_site_name;
                dtlsInfo.EntDate = ent_date;
                dtlsInfo.CurrentPosClassDate = curr_pos_class_date;

                dtlsInfo.TmpEmployee = tmp_emp_seq;
                dtlsInfo.TmpEducate = tmp_edu_seq;

                dtlsInfo.Remarks = Remarks;
                dtlsInfo.UpdatedBy = UpdatedBy;

                DAL.Update(dtlsInfo);
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            try
            {
                DAL.Delete(Sequence, UpdatedBy);
            }
            catch
            {
                throw new Exception("");
            }
        }

        public void InsertSalaryChangeAuto(int CmdSequence, int CreatedBy)
        {
            try
            {
                DAL.P_Auto_Insert_Salary_Changes(CmdSequence, CreatedBy);
            }
            catch
            {
                throw new Exception("");
            }
        }
    }
}
